package com.c.controller;

import com.c.entity.OrderSetting;
import com.c.service.OrderSettingService;
import com.c.utils.MessageConstant;
import com.c.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @author wangxiangli
 * @create 2022-12-29-16:13
 */
@RequestMapping("/ordersetting")
@RestController
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    /**
     * 根据月份查询对应的预约设置数据
     *
     * @param date
     * @return
     */
    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {//date格式为：yyyy-MM
        try {
            List<Map> list = orderSettingService.getOrderSettingByMonth(date);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }

    /**
     * 根据日期设置对应的预约设置数据
     *
     * @param orderSetting
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING')")
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }
    /**
     * 取消设置的预约
     *
     * @param date
     * @return
     */
    @PreAuthorize("hasAuthority('ORDERSETTING_CANCEL')")
    @RequestMapping("/cancelOrder")
    public Result cancelOrder(String date) {
        try {
            Result result = orderSettingService.cancelOrder(date);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.CANCEL_ORDERSETTING_FAIL);
        }
    }

    //批量导入添加医师方法
    @PostMapping("/BatchaddOrderSettings")
    public Result BatchaddDoctors(@RequestParam("excelFile") MultipartFile excelFile){
        try {
            orderSettingService.BatchaddOrderSettings(excelFile,orderSettingService);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }
}
